# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import hashlib
from urllib import parse

import scrapy
from itemloaders import ItemLoader
from itemloaders.processors import MapCompose, TakeFirst, Identity


# 自定义处理方法title
def deal_title(value):
    return value


# 处理url
def deal_img_url(value):
    return parse.urljoin("https://", value)


# 处理用户唯一码
def deal_union_code(value):
    return hashlib.md5(value.encode(encoding='UTF-8')).hexdigest()


"""
自定义ItemLoader
"""


class BlogsItemLoader(ItemLoader):
    # 默认取第一个
    # 设置成 Identity 就是取list 设置成 Join 就是可以自动分隔
    default_output_processor = TakeFirst()


class BlogsItem(scrapy.Item):
    # 标题
    title = scrapy.Field()
    # 设置图片url
    img_urls = scrapy.Field(
        input_processor=MapCompose(deal_img_url)
    )
    # 链接唯一ID
    union_code = scrapy.Field(
        input_processor=MapCompose(deal_union_code)
    )
    # 设置时间
    time = scrapy.Field()
